﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class FSelectListMediator : MyWindow
{
    GList _list;
    OneDelegate _selectCB;
    List<PackageItem> _assets;
    string _package;
    PackageItemType _itemType;

    public FSelectListMediator(){

    }
    public FSelectListMediator(string package,PackageItemType itemType,OneDelegate selectCB)
    {
        InitData(package,itemType,selectCB);
    }
    /// <summary>
    /// 初始化数据
    /// </summary>
    /// <param name="package"></param>
    /// <param name="itemType"></param>
    /// <param name="selectCB"></param>
    public void InitData(string package,PackageItemType itemType,OneDelegate selectCB){
        _package = package;
        _selectCB = selectCB;
        _itemType = itemType;
        FreshPanel();
    }
    /// <summary>
    /// 初始化面板
    /// </summary>
    protected override void OnInit()
    {
 	    base.OnInit();
        this.contentPane = UIPackage.CreateObject("CardPackage", "CardList").asCom;
        this.Center();//居中

        this.contentPane.GetChild("closeBtn").onClick.Add(() => { this.Hide(); });
        this.contentPane.GetChild("newBtn").visible = false;
        this.contentPane.GetChild("title").text = "所有可选项";

        _list = this.contentPane.GetChild("cardList").asList;
    }
    /// <summary>
    /// 刷新面板显示
    /// </summary>
    private void FreshPanel(){
        if(_list == null) return;
        //设置默认item为图片
        _list.defaultItem = UIPackage.GetItemURL("CommonPackage","ImgItem");
        _list.itemRenderer = RenderImageItem;
        //音乐类型
        if(_itemType == PackageItemType.Sound) {
            _list.defaultItem = UIPackage.GetItemURL("CommonPackage","AudiolListItem");
            _list.itemRenderer = RenderAudioItem;
        }
        _assets = MyUIPackage.GetAllPointAssets(_package,_itemType);
        _list.numItems = _assets.Count;
    }
	// Use this for initialization
    protected override void OnShown()
    {
 	    base.OnShown();
        this.Center();
    }

    void RenderImageItem(int index, GObject item)
    {
        item.onClick.Add(OnClickBack);
        var image = _assets[index];
        var imageUrl = "ui://" + image.owner.name + "/" + image.name;
        item.asCom.GetChild("img").asLoader.url = imageUrl;
    }

    void RenderAudioItem(int index, GObject item)
    {
        item.text = _assets[index].name;
        item.onClick.Add(OnClickBack);
        item.asCom.GetChild("play").onClick.Add((context)=>{
            var _index = _list.GetChildIndex(((GObject)context.sender).parent);
            // Debug.Log(GRoot.inst.soundVolume.ToString());
            var _audio = Stage.inst.gameObject.GetComponent<AudioSource>();
            //先停止之前的音乐
            _audio.Stop();
            GRoot.inst.PlayOneShotSound(_assets[_index].audioClip,0.5f);
            context.StopPropagation();
        });
    }

    private void OnClickBack(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.sender);
        if (_selectCB != null) {
            var asset = _assets[index];
            var assetUrl = "ui://" + asset.owner.name + "/" + asset.name;
            _selectCB(assetUrl);
        }
        this.Hide();
    }
	// Update is called once per frame
	void Update () {
		
	}
}
